package com.数据结构与算法.sparseArray;

public class SparseArray {
    public static void main(String[] args) {
        int a[][] = new int[11][11];
        a[1][2] = 1;
        a[2][3] = 2;
        a[7][9] = 3;
        for (int[] row : a) {
            for(int data : row){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
        int sum = 0;
        for (int i=0;i<11;i++) {
            for(int j=0;j<11;j++){
                if(a[i][j]!=0){
                    sum++;
                }
            }
        }
        int sparseArr[][] = SparseArr(sum,a);

        A2(sparseArr);
    }

    public static int[][] SparseArr(int sum,int[][] a){
        int sparseArr[][] = new int [sum+1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;
        int count = 0;
        for (int i=0;i<11;i++) {
            for(int j=0;j<11;j++){
                if(a[i][j] != 0){
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = a[i][j];
                }
            }
        }

        for (int i=0;i<sparseArr.length;i++){
            System.out.printf("%d\t%d\t%d\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
        }
        return sparseArr;
    }

    public static int[][] A2(int[][] sparseArr){
        int a2[][] = new int [sparseArr[0][0]][sparseArr[0][1]];
        for (int i=1;i<sparseArr.length;i++){
            a2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        for (int[] row : a2) {
            for(int data : row){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
        return a2;
    }
}
